#define _CRT_SECURE_NO_WARNINGS
#include"queue.h"

void QueueInit(Queue* q)
{
	assert(q);
	q->head = q->tail = NULL;
	q->k = 0;
}

void QueuePush(Queue* q, QDatatype data)
{
	assert(q);
	QNode* tmp = (QNode*)malloc(sizeof(QNode));
	if (tmp == NULL)
	{
		perror("malloc fail");
		return;
	}
	tmp->_data = data;
	tmp->_next = NULL;
	if (q->tail == NULL)
	{
		q->head = q->tail = tmp;
	}
	else
	{
		q->tail->_next = tmp;
		q->tail = tmp;
	}
	q->k++;
}

void QueuePop(Queue* q)
{
	assert(q);
	assert(q->k > 0);
	QNode* next = q->head->_next;
	free(q->head);
	q->head = next;
	if (q->head == NULL)
	{
		q->tail = NULL;
	}
	q->k--;
}

QDatatype QueueFront(Queue* q)
{
	assert(q);
	assert(q->k > 0);
	return q->head->_data;
}

QDatatype QueueBack(Queue* q)
{
	assert(q);
	assert(q->k > 0);
	return q->tail->_data;
}

int QueueSize(Queue* q)
{
	assert(q);
	return q->k;
}

bool QueueEmpty(Queue* q)
{
	assert(q);
	return q->tail == NULL;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* tmp = q->head;
	while (tmp)
	{
		QNode* next = tmp->_next;
		free(tmp);
		tmp = next;
	}
	q->k = 0;
	q->head = q->tail = NULL;
}